package org.example.Hashtable;

import java.util.HashMap;

public class Adding_Four_Numbers_II {
    public static void main(String[] args) {
        //四数相加 II

        //给你四个整数数组 nums1、nums2、nums3 和 nums4 ，数组长度都是 n ，请你计算有多少个元组 (i, j, k, l) 能满足：
        int[] nums1 = {1,2};
        int[] nums2 = {-2,-1};
        int[] nums3 = {-1,2};
        int[] nums4 = {0,2};
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        int result = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int sum = nums1[i] + nums2[j];
                if (hashMap.containsKey(sum)){
                    Integer sum12 = hashMap.get(sum);
                    sum12 +=1;
                    hashMap.put(sum,sum12);
                }else{
                    hashMap.put(sum,1);
                }
            }
        }
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int sum34 = nums3[i] + nums4[j];
                if (hashMap.containsKey(0-sum34)){
                    result+=hashMap.get(0-sum34);
                }
            }
        }
        System.out.println(result);

    }
}
